package controller;

import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.servlet.ServletException;
import utils.JSFUtils;

/**
 *
 * @author ZimmE
 */
@Stateless
@LocalBean
public class SessionController {

    /**
     * Checks if a user is authenticated
     *
     * @return
     * <code>true</code> if user is authenticated, else
     * <code>false</code>
     */
    public boolean isAuthenticated() {
        return JSFUtils.getRequest().getUserPrincipal() != null;
    }

    /**
     * Tries to log out authenticated users
     */
    public void logout() {
        try {
            JSFUtils.getRequest().logout();
        } catch (ServletException ex) {
            Logger.getLogger("MyGrona.error").log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Gets current users
     * <code>Principal</code>
     *
     * @return Authenticated users principal
     */
    public Principal getPrincipal() {
        return JSFUtils.getRequest().getUserPrincipal();
    }
}
